import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

// Element Plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 自定义样式
import './assets/styles/global.scss'

const app = createApp(App)

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 在路由初始化前恢复登录状态
const restoreLoginState = () => {
  console.log('恢复登录状态...')
  console.log('Store state:', store.state)
  
  // 如果有token但没有用户信息，清除token
  if (store.state.token && !store.state.userInfo) {
    console.log('发现孤立token，清除...')
    store.dispatch('logout')
  }
  
  // 如果有用户信息但没有token，也清除用户信息
  if (store.state.userInfo && !store.state.token) {
    console.log('发现孤立用户信息，清除...')
    store.dispatch('logout')
  }
  
  console.log('登录状态恢复完成, isLoggedIn:', store.getters.isLoggedIn)
}

// 恢复登录状态
restoreLoginState()

app.use(store)
app.use(router)
app.use(ElementPlus)

app.mount('#app') 